<template>
  <div>
    <el-card class="setting">
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <!-- 新增按钮 -->
          <el-row>
            <el-button @click="openAdd" type="primary"><i class="el-icon-plus"></i> 新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table :data="list" border class="mytable">
            <el-table-column label="序号" type="index" width="100%"> </el-table-column>
            <el-table-column label="角色名" prop="name"> </el-table-column>
            <el-table-column label="描述" prop="description"> </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="{ row }">
                <el-button type="text" @click="openRole(row)">分配权限</el-button>
                <el-button type="text" @click="edit(row)">修改</el-button>
                <el-button type="text" @click="del(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页功能 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="pagesizes"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <el-form ref="form" :model="form" label-width="100px" class="myform">
            <el-form-item label="企业名称">
              <el-input v-model="form.name" disabled></el-input>
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="form.companyAddress" disabled></el-input>
            </el-form-item>
            <el-form-item label="公司电话">
              <el-input v-model="form.companyPhone" disabled></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="form.mailbox" disabled></el-input>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="form.remarks" disabled></el-input>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 修改面板 添加-->
    <el-dialog :title="this.userform.id ? '编辑角色' : '新增角色'" :visible.sync="dialogShow">
      <el-form :model="userform" label-width="100px" ref="myform">
        <el-form-item label="角色名称">
          <el-input v-model="userform.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="userform.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 权限面板 -->
    <setrole ref="myrole" :roleName="roleName" :showRole.sync="showRole"></setrole>
  </div>
</template>

<script>
import { getListApi, getCompanyApi, updateRoleApi, addRoleApi, delRoleApi } from '@/api/setting'
export default {
  name: 'setting',
  data() {
    return {
      // 面板标题
      roleName: '人事经理',
      // 控制面板显示
      showRole: false,
      activeName: 'first',
      // 表格数据源
      list: [],
      // 当前页
      page: 1,
      pagesizes: [2, 4, 5, 10],
      // 页容量
      pagesize: 2,
      // 总数据
      total: 0,
      // 企业信息
      form: {},
      dialogShow: false,
      // 面板数据源
      userform: {
        name: '',
        description: ''
      }
    }
  },
  methods: {
    // 打开权限面板
    openRole(row) {
      this.showRole = true
      this.roleName = row.name
      this.$refs.myrole.getRoleId(row.id)
    },
    async getCompany() {
      // 得到公司id
      const companyId = this.$store.getters.userInfo.companyId
      // 根据id获取公司信息
      const res = await getCompanyApi(companyId)
      // console.log(res)
      this.form = res.data.data
    },
    // 页容量改变触发
    handleSizeChange(size) {
      this.pagesize = size
      this.getRoLeList()
    },
    // 当前页改变触发
    handleCurrentChange(page) {
      this.page = page
      this.getRoLeList()
    },
    // 获取角色列表
    async getRoLeList() {
      const res = await getListApi({
        page: this.page,
        pagesize: this.pagesize
      })
      // console.log(res)
      this.list = res.data.data.rows
      this.total = res.data.data.total
    },
    // 打开修改框
    edit(row) {
      this.dialogShow = true
      this.userform = { ...row }
    },
    // 打开新增
    openAdd() {
      this.dialogShow = true
    },
    // 实现修改 新增功能
    async submit() {
      if (this.userform.id) {
        await updateRoleApi(this.userform)
      } else {
        //  新增操作
        await addRoleApi(this.userform)
      }
      this.$message.success(this.userform.id ? '修改成功' : '新增成功')
      this.dialogShow = false
      this.userform = {
        name: '',
        description: ''
      }
      // 获取最新数据
      this.getRoLeList()
    },
    // 根据id删除
    async del(row) {
      this.$confirm('是否删除当前角色', '提示')
        .then(async () => {
          await delRoleApi(row.id)
          this.$message.success('删除成功')
          this.getRoLeList()
        })
        .catch(() => {})
    }
  },
  created() {
    this.getRoLeList()
    this.getCompany()
  },
  components: {
    setrole: () => import('./components/setrole.vue')
  }
}
</script>

<style lang="scss">
.setting {
  margin: 20px;
  .first {
    padding: 60px;
  }
  .mytable {
    margin: 20px 0px;
  }
}
.myform {
  width: 500px;
  margin-left: 100px;
}
</style>
